// pages/user/user.js
import {
  Base
} from '../../utils/base.js';
import {
  User
} from './user-model.js';

const app = getApp()
const user = new User()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    isFirst: false,
    threeOrderCount: null,
    isVip: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.aldstat.sendEvent('进入个人中心');
    let sessionId = wx.getStorageSync("sessionId");
    if (sessionId === "") {
      wx.redirectTo({
        url: '/pages/getUserInfo/getUserInfo?path=/pages/user/user'
      })
      return
    }
    //判断用户是否登录
    let userInfo = app.globalData.userInfo;
    let that = this
    let storeNo = 'S00000001'

    this.getOrderCount()
    //检查是否为推荐人
    //this.checkChannel();

    if (userInfo) {
      this.setData({
        userInfo: userInfo,
      })
    } else {
      wx.getUserInfo({
        success: function (res) {
          that.setData({
            userInfo: res.userInfo
          })
        }
      })
    }

  },
  jumpOrder(e) {
    let url = user.getDataSet(e, 'url')
    let phone = wx.getStorageSync('phone')
    if (phone === '') {
      wx.navigateTo({
        url: '/pages/signin/signin?path=/pages/user/user',
      })
      return
    } else {
      wx.navigateTo({
        url
      })
    }
  },
  updateUserInfo() {
    let userInfo = wx.getStorageSync('userInfo')

    if (userInfo) {
      this.setData({
        userInfo: userInfo
      })
    }

  },
  onShow: function () {
    this.setData({
      isVip: wx.getStorageSync('userStatus')
    })
    if (this.data.isFirst) {
      this.getOrderCount()
    }
    //   if (this.data.isChannelPeople){
    //       this.channelInfo()
    //   }

  },
  getOrderCount() {
    let params = {
      data: {}
    }
    user.getOrderCount(params, res => {
      if (res.status) {
        this.setData({
          threeOrderCount: res.data,
          isFirst: true
        })
      }
    })
  },
  //检查是否为推荐人
  checkChannel() {
    let params = {
      data: {}
    }
    let that = this;
    user.checkChannel(params, res => {
      console.log(res)
      if (res.status) {
        if (res.data.isChannelPeople == 1) {
          that.setData({
            isChannelPeople: true
          })
          that.channelInfo();
        } else {
          that.setData({
            isChannelPeople: false
          })
        }
      }
    })

  },
  //个人渠道推广数据统计
  channelInfo() {
    let date = user.formate(new Date(), 'yyyy/MM/dd');
    let startTime = new Date(date + " 00:00:00").getTime();
    let endTime = new Date(date + " 23:59:59").getTime();

    let params = {
      "data": {
        "startTime": startTime,
        "endTime": endTime,
        "channel": '' + wx.getStorageSync("phone"),
      }
    }
    console.log('params==================', params)
    let that = this;
    user.channelInfo(params, res => {
      if (res.status) {
        that.setData({
          channelSum: res.data
        })
      }
    })
  },
  //跳转到订单列表页面
  jumpOrderList() {
    wx.navigateTo({
      url: '/pages/inviteOrderList/inviteOrderList',
    })
  },
  //跳转到用户列表页面
  jumpPeopleList() {
    wx.navigateTo({
      url: '/pages/invitePeopleList/invitePeopleList',
    })
  },
  call() {
    wx.makePhoneCall({
      phoneNumber: '18210197177' 
    })
  }
})